
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class Line extends Line2D implements Comparable<Line> {

    protected Point p1;
    protected Point p2;
    private int x_offset, y_offset;

    public Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public Rectangle2D getBounds2D() {
        return null;
    }

    public Point getP1() {
        return this.p1;
    }
    
    public void setP1(Point p){
        p1 = p;
    }

    public Point getP2() {
        return this.p2;
    }
    
    public void setP2(Point p){
        p2 = p;
    }

    public double getX1() {
        return this.p1.getX();
    }

    public double getX2() {
        return this.p2.getX();
    }

    public double getY1() {
        return this.p1.getY();
    }

    public double getY2() {
        return this.p2.getY();
    }
    
    public void setOffset(int x_offset, int y_offset){
        this.x_offset = - x_offset;
        this.y_offset = - y_offset;
    }
   
    public int getXOffset(){
        return x_offset;
    }
   
    public int getYOffset(){
        return y_offset;
    }
    
    /*public boolean contains(Point p) {
        // TODO Ajouter delta
        if(this.getY1() >= p.getY() && this.getY2() <= p.getY()) {
            java.lang.Double x = getXForY(p.getY()); 
            return ((1E-8 > Math.abs(x - p.getX())) || x.isNaN());
        }
        return false;
    }/**/
    
    public boolean contains(Point p) {
        return this.intersects(p.x, p.y, 1E-8, 1E-8);
    }

    public void setLine(double x1, double y1, double x2, double y2) {
        this.p1 = new Point(x1, y1);
        this.p2 = new Point(x2, y2);
    }

    public double getXForY(double y) {
        double[] coeffs = this.getCoefficents();
        
        if(coeffs.length == 2)
            return (y - coeffs[1]) / coeffs[0];
        else
            return coeffs[0];
    }
    
    public double[] getCoefficents() {
        if(getX1() != getX2()) {
            double a = (getY1() - getY2()) / (getX1() - getX2());
            double b = getY2() - ((getY1() - getY2()) / (getX1() - getX2())) * getX2();
            return new double[]{a, b};
        } else {
            double c = getX1();
            return new double[]{c};
        }
    }
    
    @Override
    public int compareTo(Line other) {
        double current_y = other.getY1();
        double current_x = this.getXForY(current_y);
        if((new java.lang.Double(current_x)).isNaN())
            current_x = getX1();
        // TODO Ajouter valeur Delta
        
//        System.out.println("X1 " + current_x + "  X other " + other.getX1());
//        System.out.printf("%s%.10f\n", "Diff Xs ", Math.abs(current_x - other.getX1()));
        if(1E-8 > Math.abs(current_x - other.getX1())) { // other.getX1() == current_x - même origine car Y1 "fixé"
            double current_x1, current_x2;
            double current_y2 = Math.max(this.getY2(), other.getY2());
            if(current_y2 == this.getY2()) {
                current_x1 = this.getX2();
                current_x2 = other.getXForY(current_y2);
                if((new java.lang.Double(current_x2)).isNaN())
                    current_x2 = other.getX2();
            } else {
                current_x1 = this.getXForY(current_y2);
                current_x2 = other.getX2();
                if((new java.lang.Double(current_x1)).isNaN())
                    current_x2 = this.getX2();
            }
            
//            System.out.println("X11 " + current_x1 + "  X12 " + current_x2);
//            System.out.printf("%s%.10f\n", "Diff Xs ", Math.abs(current_x2 - current_x1));
//            System.out.println("TEST " + (1E-8 > Math.abs(current_x1 - current_x2)));
            if(1E-8 > Math.abs(current_x2 - current_x1)) return 0;
            else if(current_x1 > current_x2) return -1;
            else return 1;
        }
        else if(current_x > other.getX1())
            return -1;
        else //if(current_x < other.getX1())
            return 1;
    }
    
    @Override
    public String toString() {
        return "p1 : " + this.getP1() + "  p2 : " + this.getP2();
    }
    
}
